<?php
!defined('IN_AM') && exit('Access Denied');
class base {
	var $db;
	var $view;
	function __construct()
	{
		$this->base();
	}
	function base() {
		$this->init_db();
		$this->init_user();
		$this->init_template();
	}
	function init_db(){
		require_once AM_ROOT.'/lib/db.class.php';
		$this->db = new db();
		$this->db->connect(AM_DBHOST,AM_DBUSER,AM_DBPW,AM_DBNAME,AM_DBCHARSET,AM_DBCONNECT,AM_DBTABLEPRE);
	}
		
	function init_template(){
		require_once AM_ROOT.'lib/smarty/Smarty.class.php';
		$this->view = new smarty();
		//所有页面数据库查询语句
		$this->view->assign('dbhistories',$this->db->histories);
		//统计页面数据库查询次数
		$this->view->assign('dbquerynum',$this->db->querynum);
		$this->view->template_dir = AM_ROOT.'view/default/';
		$this->view->compile_dir = AM_ROOT.'cache/template_c/';
		$this->view->cache_dir = AM_ROOT.'cache/';
		$this->view->request_use_auto_globals= true;
		$this->view->left_delimiter = '{{';
		$this->view->right_delimiter='}}'; 
	}
	//初始化用用户
	function init_user(){
		session_start();
		$uid = $_SESSION['user_id']? $_SESSION['user_id']:0;
		$newpm = $this->db->fetch_first("SELECT count(new) FROM ".AM_DBTABLEPRE."pms WHERE msgtoid={$uid} AND new=1 limit 0,1");
		$newpm = $newpm['count(new)'];
		$_SESSION['newpm'] = $newpm;
	}
	function redirect($url,$time,$message) {
	header("Content-type: text/html; charset=utf-8");
	echo"<script>";
	echo"function redirect() {window.location.replace('$url');}\n";
	echo"setTimeout('redirect();',".$time.");\n";
	echo"</script>";
	echo '<div id=tip style="position: absolute; top: 30%;left:34%;height:200px;width:330px;rder-bottom: 25px solid red; border: 1px solid #99CCFF;">';
	echo '<dl style="margin:0;">';
	echo '<dt style="text-align:center;padding-top:8px;background-color:#99CCFF;height:20px">提示信息</dt>';
	echo '	<dd style="padding:10px 0 4px 8px;font-weight:900;"><img src="./images/icon_error.jpg">'.$message.'</dd>';
	echo '	<dd style="text-align:left; padding:5px 0 0 0;">浏览器会自动跳转页面,无需人工干预.</dd>';
	echo "<dd><a href=\"$url\">除非当您的浏览器没有自动跳转时,请点击这里</a></dd>";
	echo '</dl>';
	echo '</div>';
}
	function load_class($model){
		require_once AM_ROOT."model/$model.php";
		eval('$_ENV[$model] = new '.$model.'model($this);');
		return $_ENV[$model];
	}
	
	
		/**
	 * 翻页函数
	 *
	 * @param int $num 总纪录数
	 * @param int $perpage 每页大小
	 * @param int $curpage 当前页面
	 * @param string $mpurl url
	 * @param string $name 页码名称	 
	 * @return string 类似于: <div class="page">***</div>
	 */
	function multi($num, $perpage, $curpage, $mpurl,$name='page') {
		$page = 5;
		$multipage = '';
		$mpurl .= strpos($mpurl, '?') ? '&' : '?';
		if($num > $perpage) {
			$offset = 2;
			$pages = @ceil($num / $perpage);
			if($page > $pages) {
				$from = 1;
				$to = $pages;
			} else {
				$from = $curpage - $offset;
				$to = $from + $page - 1;
				if($from < 1) {
					$to = $curpage + 1 - $from;
					$from = 1;
					if($to - $from < $page) {
						$to = $page;
					}
				} elseif($to > $pages) {
					$from = $pages - $page + 1;
					$to = $pages;
				}
			}
			$multipage = ($curpage - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.$name.'=1" class="first">1 ...</a>' : '').
				($curpage > 1 ? '<a href="'.$mpurl.$name.'='.($curpage - 1).'" class="prev">&lsaquo;&lsaquo;</a>' : '');
			for($i = $from; $i <= $to; $i++) {
				$multipage .= $i == $curpage ? '<strong>'.$i.'</strong>' :
					'<a href="'.$mpurl.$name.'='.$i.'">'.$i.'</a>';
			}
			$multipage .= ($curpage < $pages ? '<a href="'.$mpurl.$name.'='.($curpage + 1).'" class="next">&rsaquo;&rsaquo;</a>' : '').
				(0 && $to < $pages ? '<a href="'.$mpurl.$name.'='.$pages.'" class="last">... '.$realpages.'</a>' : '');
			$multipage = $multipage ? $multipage : '';
		}
		return $multipage;
	}

	/**
	 * 对翻页的起始位置进行判断和调整
	 *
	 * @param int $page 页码
	 * @param int $ppp 每页大小
	 * @param int $totalnum 总纪录数
	 * @return unknown
	 */
	function page_get_start($page, $ppp, $totalnum) {
		$totalpage = ceil($totalnum / $ppp);
		$page =  max(1, min($totalpage, intval($page)));
		return ($page - 1) * $ppp;
	}
}
?>